package com.huawei.petSystemdemo.ui.person;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.GlobalViewModel;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.bean.User;
import com.huawei.petSystemdemo.ui.my.MyViewModel;
import com.huawei.petSystemdemo.utils.MyRequest;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Personal_page extends AppCompatActivity {
    GlobalViewModel global;
    private MyViewModel myViewModel;
    EditText info_name;
    EditText info_qq;
    EditText info_tel;
    EditText info_sign;
    TextView info_username;
    String username;

    String sign = "";
    String nnname = "";
    String qq = "";
    String tel = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_page);
        this.getSupportActionBar().hide();


        //global = new ViewModelProvider(this).get(GlobalViewModel.class); 这行已经会报错了
        global = GlobalViewModel.getGlobal();

        info_name = findViewById(R.id.info_name);
        info_qq = findViewById(R.id.info_qq);
        info_tel = findViewById(R.id.info_tel);
        info_sign = findViewById(R.id.info_sign);
        info_username = findViewById(R.id.info_username);
        initinfo();//初始化修改页面的各个数据
        TextView info_modify = findViewById(R.id.info_modify);
        info_modify.setOnClickListener(v->{

            if(ChickInput()){

                Map<String,Object> requestBody = new HashMap<>();
                requestBody.put("username",username);
                requestBody.put("name",info_name.getText().toString());
                requestBody.put("tel",info_tel.getText().toString());
                requestBody.put("qq",info_qq.getText().toString());
                requestBody.put("sign",info_sign.getText().toString());
                System.out.println(requestBody);
                MyRequest.post("/user/infomodify", requestBody, new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                        Looper.prepare();
                        System.out.println("连接服务器失败!");
                        Toast.makeText(Personal_page.this,"连接服务器失败!",Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        String json = response.body().string();
                        System.out.println(json);
                        JSONObject jsonObject = JSON.parseObject(json);
                        int statusCode = jsonObject.getInteger("statusCode");
                       Personal_page.this.finish();

                    }
                });
            }

        });
    }

    public void onClick (android.view.View back){
//        Intent intent = new Intent(this, MyFragment.class);//界面跳转
//        startActivity(intent);
       finish();
    }

    public boolean ChickInput(){
        String name = info_name.getText().toString();
        String tel = info_tel.getText().toString();
        String sign = info_sign.getText().toString();
        String qq = info_qq.getText().toString();
        //trim() 方法用于删除字符串的头尾空白符
        if(name.equals("")) {
            Toast.makeText(Personal_page.this,"网名不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(tel.equals("")) {
            Toast.makeText(Personal_page.this,"电话不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(sign.equals("")) {
            Toast.makeText(Personal_page.this,"个性签名不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(qq.equals("")) {
            Toast.makeText(Personal_page.this,"qq不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    public void initinfo(){

        Intent intent = getIntent();
        username = intent.getStringExtra("username");
        info_username.setText(username);
        System.out.println("username:" + username);
        Map<String,Object> requestBody = new HashMap<>();
        requestBody.put("username",username);
        MyRequest.get("/user/getByUsername", requestBody, json -> {
            JSONObject jsonObject = JSON.parseObject(json);
            System.out.println(jsonObject);
            if(jsonObject != null){
                User user = jsonObject.getObject("data", User.class);//可以把data转化成user对象
                sign = user.getSignature();
                nnname = user.getName();
                qq = user.getQq();
                tel = user.getTel();
                //String tel = jsonObject.getString("tel");
                System.out.println(sign + nnname + qq + tel);
                //Toast.makeText(Personal_page.this,"信息修改成功",Toast.LENGTH_SHORT).show();
                System.out.println("信息修改成功");
                //Looper.prepare();
                info_sign.setText(sign);
                info_qq.setText(qq);
                info_tel.setText(tel);
                info_name.setText(nnname);
            }else {
                System.out.println("getByUsername 获取json失败");
            }

        });



    }

}